#define _CRT_SECURE_NO_WARNINGS 1
#include "ObjectPool.h"
#include "ConcurrentAlloc.h"

void Alloc1()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
	}
}

void Alloc2()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(7);
	}
}


void TestConcurrentAlloc1()
{
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(1);
	void* p4 = ConcurrentAlloc(7);
	void* p5 = ConcurrentAlloc(8);

	cout << p1 << endl;
	cout << p2 << endl;
	cout << p3 << endl;
	cout << p4 << endl;
	cout << p5 << endl;
}

void TestConcurrentAlloc2()
{
	for (size_t i = 0; i < 1024; ++i)
	{
		void* p1 = ConcurrentAlloc(6);
		cout << p1 << endl;
	}

	void* p2 = ConcurrentAlloc(8);
	cout << p2 << endl;
}

void MultiThreadAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void MultiThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(16);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void BigAlloc()
{
	void* p1 = ConcurrentAlloc(257 * 1024);
	ConcurrentFree(p1);

	void* p2 = ConcurrentAlloc(129 * 8 * 1024);
	ConcurrentFree(p2);
}

void Test_ConcurrentAlloc1()
{
	int* p1 = (int*)ConcurrentAlloc(6); //maxsize:2 allocnum:1 size:0
	int* p2 = (int*)ConcurrentAlloc(7); //3 2 1
	int* p3 = (int*)ConcurrentAlloc(8); //3   0
	int* p4 = (int*)ConcurrentAlloc(1); //4 3 2
	int* p5 = (int*)ConcurrentAlloc(1); //4   1
	int* p6 = (int*)ConcurrentAlloc(1); //4   0
	int* p7 = (int*)ConcurrentAlloc(1); //5 4 3


	*p1 = 10;
	*p2 = 20;
	*p3 = 30;
	*p4 = 40;

	cout << p1 << ": " << *p1 << endl;
	cout << p2 << ": " << *p2 << endl;
	cout << p3 << ": " << *p3 << endl;
	cout << p4 << ": " << *p4 << endl;

	ConcurrentFree(p1); //maxsize:5 size:4 span._usecount:10
	ConcurrentFree(p2); //5 0 5
	ConcurrentFree(p3); //5 1 5
	ConcurrentFree(p4); //5 2 5
	ConcurrentFree(p5); //5 3 5
	ConcurrentFree(p6); //5 4 5
	ConcurrentFree(p7); //5 0 0 
}

void Test_BigAlloc()
{
	int* p1 = (int*)ConcurrentAlloc(MAX_BYTES + 100);
	int* p2 = (int*)ConcurrentAlloc(NPAGES << PAGE_SHIFT);

	*p1 = 10;
	*p2 = 20;

	cout << p1 << ": " << *p1 << endl;
	cout << p2 << ": " << *p2 << endl;

	ConcurrentFree(p1);
	ConcurrentFree(p2);
}




//int main()
//{
//	//TestObjectPool();
//	//TestConcurrentAlloc1();
//	//MultiThreadAlloc1();
//	MultiThreadAlloc2();
//	//BigAlloc();
//	return 0;
//}